use "Output Data\regdata_analysis.dta", clear

rename *, lower
drop if year < 2000

egen comp_agency = group(companyid agencyid)
egen agency_year = group(agencyid year)
egen comp_year = group(companyid year)
egen agency_ind = group(agencyid naics)
egen agency_ind_year = group(agencyid naics year)

tsset comp_agency year

gen log_res = log(restrictions+1)
replace log_res = 0 if missing(log_res) & year <= 2019
gen L1_log_res = log(l1_restrictions+1)
replace L1_log_res = 0 if missing(L1_log_res) & year <= 2019
gen F1_log_res = log(f1_restrictions+1)
replace F1_log_res = 0 if missing(F1_log_res) & year <= 2019


reghdfe connect_num L1_log_res log_res F1_log_res if year <= 2018, a(agency_year comp_year comp_agency) vce(cluster comp_agency)
estimates store a1
egen Lzlog_res = std(L1_log_res) if _est_a1 == 1
egen zlog_res = std(log_res) if _est_a1 == 1
egen Fzlog_res = std(F1_log_res) if _est_a1 == 1

reghdfe connect_num Lzlog_res zlog_res Fzlog_res if year <= 2018 & _est_a1 == 1, a(comp_agency) vce(cluster comp_agency)
estimates store m1
reghdfe connect_num Lzlog_res zlog_res Fzlog_res if year <= 2018 & _est_a1 == 1, a(comp_year comp_agency) vce(cluster comp_agency)
estimates store m2
reghdfe connect_num Lzlog_res zlog_res Fzlog_res if year <= 2018 & _est_a1 == 1, a(agency_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m3

#delimit ;
esttab m1 m2 m3 using output.csv, replace
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2;
#delimit cr

sum connect_num if _est_m1 == 1






drop Lzlog_res zlog_res Fzlog_res

reghdfe connect_num L1_log_res log_res F1_log_res if year <= 2018 & restrictions > 0 & !missing(restrictions), a(agency_year comp_year comp_agency) vce(cluster comp_agency)
estimates store a1
egen Lzlog_res = std(L1_log_res) if _est_a1 == 1
egen zlog_res = std(log_res) if _est_a1 == 1
egen Fzlog_res = std(F1_log_res) if _est_a1 == 1

reghdfe connect_num Lzlog_res zlog_res Fzlog_res if year <= 2018 & restrictions > 0 & !missing(restrictions) & _est_a1 == 1, a(comp_agency) vce(cluster comp_agency)
estimates store m1
reghdfe connect_num Lzlog_res zlog_res Fzlog_res if year <= 2018 & restrictions > 0 & !missing(restrictions) & _est_a1 == 1, a(comp_year comp_agency) vce(cluster comp_agency)
estimates store m2
reghdfe connect_num Lzlog_res zlog_res Fzlog_res if year <= 2018 & restrictions > 0 & !missing(restrictions) & _est_a1 == 1, a(agency_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m3

#delimit ;
esttab m1 m2 m3 using output.csv, append
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2;
#delimit cr

sum connect_num if _est_m1 == 1